﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections;

namespace RoundupReadyPlusWPF.AppCode
{
     /// <summary>
    /// Singleton para mudanca de pagina
    /// </summary>
    public sealed class PageSwitcher : Observable<PageSwitcher>
    {
        private static readonly PageSwitcher instance = new PageSwitcher();

        public enum Pages { None = 0, Region, State, District, Town, Filters, Table, Map }

        // lista com as paginas
        private UserControl[] pages;

        // define propriedade da pagina corrente internamente
        private Pages currentPage;

        // Propriedade readonly
        public Pages CurrentPage
        {
            get
            {
                return currentPage;
            }

            private set
            {
                currentPage = value;
                Notify(this);
            }
        }

        private PageSwitcher()
        {
            pages = new UserControl[Enum.GetNames(typeof(Pages)).Length];
            CurrentPage = Pages.None;
        }

        /// <summary>
        /// Intancia do singleton
        /// </summary>
        public static PageSwitcher Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Proxima pagina
        /// </summary>
        /// <returns></returns>
        public UserControl NextPage(){

            UserControl nextPage = null;
            
            // se a pagina ainda esta dentro do array incrementa
            // para a proxima, caso contrario fica na ultima
            if ((int)CurrentPage < pages.Length - 1)
            {
                CurrentPage++;
            }

            // se a pagina corrente ainda nao esta definida
            if (pages[(int)CurrentPage] == null)
            {
                // cria a pagina
                nextPage = PageFactory.NewPage(CurrentPage);
                pages[(int)CurrentPage] = nextPage;
            }
            else
            {
                // caso contrario retoran a pagina corrente
                nextPage = pages[(int)CurrentPage];
            }

            

            return nextPage;
        }

        /// <summary>
        /// Seleciona uma pagina
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public UserControl SelectPage(Pages page)
        {
            CurrentPage = page;
            return (UserControl) pages[(int)page];
        }

        /// <summary>
        /// Retorna verdadeiro se existe pagina criada
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public bool HasPage(Pages page)
        {
            return pages[(int)page] != null;
        }


    }
}
